package com.lnsoft.dqfs.server.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.RandomAccessFile;

public class DeCrypt {

    public static RandomAccessFile byteBuffToFile(String fileName, ByteBuf byteBuf) throws Exception {
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(fileName, "rw");
            //  初始化解密
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
            //  密码
            byte[] secKey = "www.easyele.com.".getBytes();
            SecretKeySpec secretKeySpec = new SecretKeySpec(secKey, "AES");
            cipher.init(Cipher.DECRYPT_MODE,secretKeySpec);

            int writerIndex = byteBuf.writerIndex();
            int readerIndex = byteBuf.readerIndex();

            //  标记 每次解密数据的 字节的数量
            int flag = 0;
            //  计算本次上传了多少组 '完整的数据'；(一组数据代表:每个时刻的金属腐蚀电流相关的遥测数据)
            int group = (writerIndex - readerIndex) / 320;
            //  剩余的半组数据
            int remainderBytes = (writerIndex - readerIndex) % 320;
            //  解密次数
            int round = group * 2;
            if (remainderBytes == 176) {
                round += 1;
            }
            //  每次写入文件时的起点
            long position = 0;
            //  开始解密和写入解密完的数据到文件
            for (int index = 0; index < round; index++) {
                byte[] encryptBytes = null;
                if (flag == 0) {
                    encryptBytes = new byte[176];
                    flag = 1;
                } else {
                    encryptBytes = new byte[144];
                    flag = 0;
                }
                byteBuf.readBytes(encryptBytes);
                System.out.println("解密之前的字节：");
                System.out.println(Arrays16.toString(encryptBytes));

                //  使用标准解密方法解密
                byte[] original = cipher.doFinal(encryptBytes);

                System.out.println("解密之后的字节：");
                System.out.println(Arrays16.toString(original));

                int zeroIndex = original.length;
                for (int i = zeroIndex - 1; i > -1; i--) {
                    if (original[i] != 0) {
                        zeroIndex = i + 1;
                        break;
                    }
                }

                ByteBuf byteBuf1 = Unpooled.wrappedBuffer(original);
                byteBuf1.readBytes(randomAccessFile.getChannel(), position, zeroIndex);
                position += zeroIndex;

                System.out.println("\n");
                String originalString = new String(original, "utf-8");
                System.out.println(originalString);
                System.out.println("\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                randomAccessFile.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return randomAccessFile;
    }

//    public static byte[] decryptByteArray(byte[] encryptByte) {
//        byte[] temp = null;
//
//    }

}
